package com.sfac.day03.modules.account.dao;

import com.sfac.day03.modules.account.entity.User;
import com.sfac.day03.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserDao {
    @Select("select * from account_user where user_name=#{userName} and password=#{password}")
    public abstract User getUserByUserNameAndPassword(@Param("userName") String userName,@Param("password") String password);

    @Select("select * from account_user where user_name=#{userName}")
    public abstract List<User> getUserByUserName(String userName);

    @Select("select * from account_user where user_name=#{userName} or email = #{email} ")
    public abstract List<User> getUserByUserNameOrEmail(@Param("userName") String userName,@Param("email") String email);

    @Insert("insert into account_user (create_date,email,user_name,password,user_image)"
    +"values(#{createDate},#{email},#{userName},#{password},#{userImage})")
    public abstract int insertUser(User user);

    @Update("update account_user set create_date=#{createDate},email=#{email},user_name=#{userName},password=#{password},user_image=#{userImage} where id=#{id} ")
    public abstract int updateUser(User user);

    @Update("update account_user set password=#{password} where id = #{id}")
    public abstract int updateUserPassword(@Param("password") String password,@Param("id") Integer id);

    @Select("select * from account_user where id = #{id}")
    public abstract User getUserById(Integer id);

    @Delete("delete from account_user where id = #{id}")
    public abstract int deleteUserById(Integer id);

    @Select("<script>"
            + "select * from account_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (email like '%${keyword}%' or "
            + " user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    public abstract List<User> getUsersBySearchVo(SearchBean searchBean);




}

